/*
Author : Nguyen Van Ty
Email  : ngvanty03@gmail.com
Date   : 6/25/2011 

*/

using System;
using System.Collections.Generic;
using System.Text;
using log4net;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using BusinessObject;
using Utility;
using System.Xml;        
namespace DataAccessLayer{
      /// <summary>
    /// Define SubCategory_TransDaoBase class. This class user for DataAccessLayer, contain 4 function Insert, Update, Search, Delete.
    /// If you want add new function, you must define in SubCategory_TransDao class , not here.
    /// </summary>
    public class SubCategory_TransDaoBase {
    private static ILog logger = null;
            #region Constructor
             /// <summary>
            /// Define Constructor.
             /// </summary>
            public SubCategory_TransDaoBase(){}
             static SubCategory_TransDaoBase()
            {
            //Init logger.
            logger = LoggingUtility.GetLogger(typeof(SubCategory_TransDaoBase).FullName);
            }
 #endregion
              /// <summary>
             /// Read data from IDataReader, convert to SubCategory_Trans Object.
            /// </summary>
            private static SubCategory_Trans GetSubCategory_TransByDataReader(IDataReader reader)
    {
      String methodName = "GetSubCategory_TransByDataReader";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
        try
      {
        SubCategory_Trans result = new SubCategory_Trans();
              //read SubCategoryId field data.
             
            result.SubCategoryId=ManagerDao.GetintDataFromDataReader(reader,"SubCategoryId");
            
              //read SubCategoryName field data.
             
            result.SubCategoryName=ManagerDao.GetstringDataFromDataReader(reader,"SubCategoryName");
            
              //read CreatedBy field data.
             
            result.CreatedBy=ManagerDao.GetstringDataFromDataReader(reader,"CreatedBy");
            
              //read CreatedDate field data.
             
            result.CreatedDate=ManagerDao.GetdatetimeDataFromDataReader(reader,"CreatedDate");
            
              //read LastUpdatedBy field data.
             
            result.LastUpdatedBy=ManagerDao.GetstringDataFromDataReader(reader,"LastUpdatedBy");
            
              //read LastUpdatedDate field data.
             
            result.LastUpdatedDate=ManagerDao.GetdatetimeDataFromDataReader(reader,"LastUpdatedDate");
            
              //read LanguageId field data.
             
            result.LanguageId=ManagerDao.GetstringDataFromDataReader(reader,"LanguageId");
            
             return result;
      }
      catch (Exception e)
      {
        // write error to log file.
        logger.Error(methodName, e);
        return null;
      }
    }
              /// <summary>
        /// Define function Insert for SubCategory_Trans Object.
        /// </summary>
        /// <param name="obj">Object Insert.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool Insert(SubCategory_Trans obj)
            {
                bool result = false;
              String methodName = "InsertSubCategory_Trans";
              if (logger.IsDebugEnabled)
              {
                LoggingUtility.logMethodEntry(logger, methodName);
              }
              DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertSubCategory_Trans");            
            
                 //Add value "SubCategoryId"  paramater.
				ManagerDao.AddInParameter(cmd, "SubCategoryId", DbType.Int32,obj.SubCategoryId);
                 //Add value "SubCategoryName"  paramater.
				ManagerDao.AddInParameter(cmd, "SubCategoryName", DbType.String,
					ConvertValueObject.CastObjValueToInsertDB(obj.SubCategoryName));
                 //Add value "CreatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,
					ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
                 //Add value "CreatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,
					ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
                 //Add value "LastUpdatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,
					ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
                 //Add value "LastUpdatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,
					ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(cmd, "LanguageId", DbType.AnsiString,obj.LanguageId);    
            //add paramater store return value.        
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
            try
              {
                ManagerDao.ExecuteNonQuery(cmd);
                int id = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
                if (id>0)//Successfull.
                {
                  result = true;
                }
                else//Error.
                {
                  result = false;
                }
              }
              catch (Exception e)
              {
                //write error message to log file.
                logger.Error(methodName, e);
                result = false;
              }
              return result;
            }
             
               /// <summary>
        /// Define function Update for SubCategory_Trans Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool Update(SubCategory_Trans obj)
          {
          bool result = false;
          String methodName = "UpdateSubCategory_Trans";
          if (logger.IsDebugEnabled)
          {
            LoggingUtility.logMethodEntry(logger, methodName);
          }
             DbCommand cmd = ManagerDao.GetStoredProcCommand("spUpdateSubCategory_Trans");
           

                 //Add value "SubCategoryId"  paramater.
				ManagerDao.AddInParameter(cmd, "SubCategoryId", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.SubCategoryId));
                 //Add value "SubCategoryName"  paramater.
				ManagerDao.AddInParameter(cmd, "SubCategoryName", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.SubCategoryName));
                 //Add value "CreatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
                 //Add value "CreatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
                 //Add value "LastUpdatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
                 //Add value "LastUpdatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(cmd, "LanguageId", DbType.AnsiString,ConvertValueObject.CastObjValueToInsertDB(obj.LanguageId));           
            // add parameter store return value.
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
           try
          {
            ManagerDao.ExecuteNonQuery(cmd);
            int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
            if (rows == 1)//Successfull.
            {
              result = true;
            }
            else//Error.
            {
              result = false;
            }

          }
          catch (Exception e)
          {
            //write error Message to log file.
            logger.Error(methodName, e);
            result = false;
          }
          return result;
                }
               /// <summary>
        /// Define function InsertUpdate for SubCategory_Trans Object.
        /// </summary>
        /// <param name="obj">Object Update.</param>
        /// <returns>
        ///    True: Successfull.
        ///    False: Process Error.
        ///</returns>
            public static bool InsertUpdate(SubCategory_Trans obj)
          {
          bool result = false;
          String methodName = "InsertUpdateSubCategory_Trans";
          if (logger.IsDebugEnabled)
          {
            LoggingUtility.logMethodEntry(logger, methodName);
          }
             DbCommand cmd = ManagerDao.GetStoredProcCommand("spInsertUpdateSubCategory_Trans");
           

                 //Add value "SubCategoryId"  paramater.
				ManagerDao.AddInParameter(cmd, "SubCategoryId", DbType.Int32,ConvertValueObject.CastObjValueToInsertDB(obj.SubCategoryId));
                 //Add value "SubCategoryName"  paramater.
				ManagerDao.AddInParameter(cmd, "SubCategoryName", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.SubCategoryName));
                 //Add value "CreatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedBy", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.CreatedBy));
                 //Add value "CreatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "CreatedDate", DbType.DateTime,ConvertValueObject.CastObjValueToInsertDB(obj.CreatedDate));
                 //Add value "LastUpdatedBy"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedBy", DbType.String,ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedBy));
                 //Add value "LastUpdatedDate"  paramater.
				ManagerDao.AddInParameter(cmd, "LastUpdatedDate", DbType.DateTime,ConvertValueObject.CastObjValueToInsertDB(obj.LastUpdatedDate));
                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(cmd, "LanguageId", DbType.AnsiString,ConvertValueObject.CastObjValueToInsertDB(obj.LanguageId));           
            // add parameter store return value.
             ManagerDao.AddParameter(cmd, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
           try
          {
            ManagerDao.ExecuteNonQuery(cmd);
            int rows = (int)ManagerDao.GetParameterValue(cmd, "returnValue");
            if (rows == 1)//Successfull.
            {
              result = true;
            }
            else//Error.
            {
              result = false;
            }

          }
          catch (Exception e)
          {
            //write error Message to log file.
            logger.Error(methodName, e);
            result = false;
          }
          return result;
                }
/// <summary>
/// Get Data SubCategory_Trans object. 
/// </summary>
/// <param name="subCategoryId" >This is primary key to get data.</param>
/// <param name="languageId" >This is primary key to get data.</param>
/// <returns>SubCategory_Trans object.</returns>
              
           public static SubCategory_Trans Get(int subCategoryId,string languageId)
    {
      String methodName = "GetSubCategory_Trans";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }
      try
      {
        SubCategory_Trans result = null;
        DbCommand dbCommnand = ManagerDao.GetStoredProcCommand("spGetSubCategory_Trans");
                 //Add value "SubCategoryId"  paramater.
				ManagerDao.AddInParameter(dbCommnand, "SubCategoryId", DbType.Int32,subCategoryId);
                 //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(dbCommnand, "LanguageId", DbType.AnsiString,languageId);
        IDataReader reader = ManagerDao.ExecuteReader(dbCommnand);
        if (reader.Read())
        {
          result= GetSubCategory_TransByDataReader(reader);

        }
        reader.Close();
        return result;
      }
      catch (System.Exception e)
      {
        //write error message to log file.
        logger.Error("Get", e);
        return null;
      }
    }
              /// <summary>
        /// Get All SubCategory_Trans object from table SubCategory_Trans.
        /// </summary>
        /// <returns>List SubCategory_Trans object.</returns>
             public static List<SubCategory_Trans> GetAll()
                {
                 String methodName = "GetAll";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        List<SubCategory_Trans> listSubCategory_Trans = new List<SubCategory_Trans>();
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllSubCategory_Trans");

        using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
        {
          while (reader.Read())
          {
            listSubCategory_Trans.Add(GetSubCategory_TransByDataReader(reader));
          }
          reader.Close();
        }

        return listSubCategory_Trans;
      }
      catch (Exception ex)
      {
        //write error message to log file.
        logger.Error(methodName, ex);
        return null;
      }
    }

/// <summary>
/// Delete SubCategory_Trans object. 
/// </summary>
/// <param name="SubCategoryId" >This is primary key to delete.</param>
/// <param name="LanguageId" >This is primary key to delete.</param>
/// <returns>True/False.</returns>

            public static bool Delete(int SubCategoryId,string LanguageId)
    {
      String methodName = "DeleteSubCategory_Trans";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spDeleteSubCategory_Trans");
                //Add value "SubCategoryId"  paramater.
				ManagerDao.AddInParameter(dbCommand, "SubCategoryId", DbType.Int32,SubCategoryId);
                //Add value "LanguageId"  paramater.
				ManagerDao.AddInParameter(dbCommand, "LanguageId", DbType.AnsiString,LanguageId);        
         //Add value returnValue  paramater.
        ManagerDao.AddParameter(dbCommand, "returnValue", DbType.Int32, ParameterDirection.ReturnValue, string.Empty, DataRowVersion.Default, null);
        try
        {
          ManagerDao.ExecuteNonQuery(dbCommand);        
          int parameterValue = (int)ManagerDao.GetParameterValue(dbCommand, "returnValue");
          if (parameterValue == 1)
          {
            return true;
          }
        }
        catch (Exception)
        {
          throw;
        }
        return false;
      }
      catch (Exception e)
      {
        //write error message to log file.
        logger.Error(methodName, e);
        return false;
      }
    }
              /// <summary>
        /// Get All Language of SubCategory_Trans object from table SubCategory_Trans.
        /// </summary>
        /// <returns>List SubCategory_Trans object.</returns>
             public static List<SubCategory_Trans> GetAllLanguageofItem(int SubCategoryId)
                {
                 String methodName = "GetAllLanguageofItem";
      if (logger.IsDebugEnabled)
      {
        LoggingUtility.logMethodEntry(logger, methodName);
      }

      try
      {
        List<SubCategory_Trans> listSubCategory_Trans = new List<SubCategory_Trans>();
        DbCommand dbCommand = ManagerDao.GetStoredProcCommand("spGetAllLanguageofItemSubCategory_Trans");
        
                //Add value "SubCategoryId"  paramater.
				ManagerDao.AddInParameter(dbCommand, "SubCategoryId", DbType.Int32,SubCategoryId);
        using (IDataReader reader = ManagerDao.ExecuteReader(dbCommand))
        {
          while (reader.Read())
          {
            listSubCategory_Trans.Add(GetSubCategory_TransByDataReader(reader));
          }
          reader.Close();
        }

        return listSubCategory_Trans;
      }
      catch (Exception ex)
      {
        //write error message to log file.
        logger.Error(methodName, ex);
        return null;
      }
    }
  
           
	                }
                }